package com.lifo.utils.common.date;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by why on 2017/6/29 0029.
 * 时间处理工具类
 */
public class DateUtil {

    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String HH_MM_SS = "HH:mm:ss";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public DateUtil() {
    }

    /**
     * 计算传入的时间与当前时间相差多少秒
     *
     * @param startDate 时间
     * @return 多少秒
     */
    public static int calLastedTimeSeconds(Date startDate) {
        long a = System.currentTimeMillis();
        long b = startDate.getTime();
        int c = (int) ((a - b) / 1000);
        return c;
    }

    /**
     * 计算俩个时间段相差多少秒
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 多少秒
     */
    public static int calLastedTimeSeconds(Date startDate, Date endDate) {
        long a = endDate.getTime();
        long b = startDate.getTime();
        int c = (int) ((a - b) / 1000);
        return c;
    }


    /**
     * 获取当前日期，默认格式2019-06-03
     *
     * @return Date
     */
    public static Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD);
        String dateString = formatter.format(currentTime);
        String substring = dateString.substring(0, 10);
        ParsePosition pos = new ParsePosition(0);
        Date currentTime_2 = formatter.parse(substring, pos);
        return currentTime_2;
    }

    /**
     * 获取当前时间 默认格式yyyy-MM-dd HH:mm:ss (24小时制)
     *
     * @return String 当前时间
     */
    public static String getNowDateString() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 按照对应格式获取当前时间
     *
     * @param formatStr 格式化规则 如：yyyy-MM-dd HH:mm:ss
     * @return String 当前时间
     */
    public static String getNowDateString(String formatStr) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        String dateString = formatter.format(currentTime);
        return dateString;
    }


    /**
     * 将字符串时间格式化成Date对象
     *
     * @param strDate 格式必须是yyyy-MM-dd HH:mm:ss,否则会返回null
     * @return Date
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        ParsePosition pos = new ParsePosition(0);
        Date strToDate = formatter.parse(strDate, pos);
        return strToDate;
    }

    /**
     * 将字符串时间格式化成 Date
     *
     * @param strDate   要格式化的时间
     * @param formatStr 格式化规则 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDate(String strDate, String formatStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        ParsePosition pos = new ParsePosition(0);
        Date strToDate = formatter.parse(strDate, pos);
        return strToDate;
    }

    /**
     * 将对应Date按照yyyy-MM-dd HH:mm:ss格式化成string
     *
     * @param date 传入的日期
     * @return string date
     */
    public static String dateToStr(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        ParsePosition pos = new ParsePosition(0);
        String strDate = formatter.format(date);
        return strDate;
    }

    /**
     * 将对应Date按照格式化规则格式化成string
     *
     * @param date      传入的日期
     * @param formatStr 格式化规则 如：yyyy-MM-dd HH:mm:ss
     * @return string
     */
    public static String dateToStr(Date date, String formatStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        ParsePosition pos = new ParsePosition(0);
        String strDate = formatter.format(date);
        return strDate;
    }

   /* *//**
     * 获取无间隔的当前时间字符串
     * @return
     *//*
    public static String getDateString(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }*/

   /* *//**
     * 获取30天后的日期
     *
     * @return
     *//*
    public static String getMonthDate() {
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
        Date today = new Date();
        //当前日期
        String endDate = sdf.format(today);
        //获取三十天前日期
        Calendar theCa = Calendar.getInstance();
        theCa.setTime(today);
        // +30天
        theCa.add(Calendar.DATE, +30);
        Date start = theCa.getTime();
        //三十天之后日期
        String startDate = sdf.format(start);
        return startDate;
    }*/

    /**
     * 获取相加目标天数之后得到的日期
     *
     * @param srtDate 从哪天开始计算
     * @param dayNum  增加多少天
     * @return String
     */
    public static String getDateAddDay(String srtDate, Integer dayNum) {
        Date date = DateUtil.strToDate(srtDate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // 今天+1天
        c.add(Calendar.DAY_OF_MONTH, dayNum);
        date = c.getTime();
        return DateUtil.dateToStr(date, YYYY_MM_DD_HH_MM_SS);
    }


    /**
     * 获取相加目标小时之后得到的日期
     *
     * @param strDate 从哪天开始计算
     * @param hourNum 小时
     * @return
     */
    public static String getDateAddHour(String strDate, Integer hourNum) {
        Date date = DateUtil.strToDate(strDate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY, hourNum);
        date = c.getTime();
        return DateUtil.dateToStr(date, YYYY_MM_DD_HH_MM_SS);
    }


    /**
     * 获取上月的日期
     * @return
     */
    public static String getLastMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataStr = DateUtil.getNowDateString();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(strToDate(dataStr));
        calendar.add(Calendar.MONTH, -1);
        return  formatter.format(calendar.getTime());
    }

    /**
     * 获取昨天的日期
     * @return
     */
    public static String getYesterday() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dataStr = DateUtil.getNowDateString();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(strToDate(dataStr));
        calendar.add(Calendar.DATE, -1);
        return  formatter.format(calendar.getTime());
    }

    /**
     * 获取无间隔的当前时间字符串
     * @return
     */
    public static String getDateString(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(YYYYMMDDHHMMSS);
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static void main(String[] args) {
        String date = DateUtil.getDateAddDay("2018-01-06 13:14:12", 30);
        System.out.println(date);
    }

}
